/*
 * @Author: 姚潘涛
 * @Date: 2024-09-19 14:29:51
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-09-19 17:22:28
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#pragma once

#include "xict_common/log/log.h"
#include "xict_common/utils/obfuscate.h"
#include "xict_common/utils/yalk.h"
#include <iostream>

namespace xict_common {
    namespace utils {

        static const char* yalk_config =
            AY_OBFUSCATE("{\"common\":{\"issue_number\":0,\"product_name\":"
                         "\"XICT\",\"product_"
                         "version\":\"1.0.0\",\"type\":0},\"hwid\":{\"mac\":"
                         "\"MacAddressIdentifier\"}}");

        static const char* public_key_base64 = AY_OBFUSCATE(
            "MIIBCgKCAQEAw4oRE2kB32yI6fECNJ5BxaVzbaw6MmCL3dOz/54ckQL5GG7YrnjR"
            "vkr/ron74i+VltWOlu6Con5g2XE6JvNLKYUXolUCW+MApp1sX70aIkw5DDSJHNfS"
            "/e36VIjIL/umM/6kCiEwh+pQAb/C22pZt5QxtwjXGirDd9Ae6Qehq6b4GmNRYUsk"
            "t8VlNEQcjn49zxEEPVZFk5mWWfYc/z6wXhuDnV13beAhSoOvfT+5EfWpucmjV0QT"
            "i6MIXfv3RnDwy6ErN8WtS/xIrZFeeXdAHJmLR9a1poCVWYvo85rhj9z61sWZL7T5"
            "W7lJ+j4u8nh9chN16QpQxQRvvm2Si48X9QIDAQAB");

        static const char* model_encryption_key =
            AY_OBFUSCATE("lyQZGuiS/zCqH13f31BbvR1lbnXcuonZlkTXbVadqGo=");

        inline bool ValidateLicense(const std::string& license_file) {
#ifndef ENABLE_YALK
            std::cout << "License validation is disabled." << std::endl;
            return true;
#else
            // verify the license at application startup
            bool is_valid = false;
            YALK_EVENT_TYPE ret =
                yalk_verify(yalk_config, nullptr, license_file.c_str(), nullptr,
                            nullptr, nullptr, public_key_base64, &is_valid);

            // print the result
            if (is_valid && ret == YALK_OK) {
                AINFO_F("The license is valid.");
                return true;
            } else {
                AERROR_F("The license is invalid (error code: {}).", ret);
                return false;
            }
#endif
        }
    }   // namespace utils
}   // namespace xict_common
